package com.yuanqi.java.service.impl;


import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yuanqi.java.entity.Product;
import com.yuanqi.java.entity.Supplier;
import com.yuanqi.java.mapper.ProductMapper;
import com.yuanqi.java.mapper.SupplierMapper;
import com.yuanqi.java.service.ISupplierService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 供应商 服务实现类
 * </p>
 *
 * @author author
 * @since 2025-08-26
 */
@Service

public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, Supplier> implements ISupplierService {
    @Autowired
    private ProductMapper productMapper;

    @Override
    public void bindProducts(Long supplierId, List<Long> productIds) {
        Supplier supplier = getById(supplierId);
        if (supplier == null) throw new IllegalArgumentException("供应商不存在");
        if (CollectionUtils.isEmpty(productIds))return;
        String supplierCode = supplier.getSupplierCode();
        UpdateWrapper<Product> uw = new UpdateWrapper<>();
        uw.in("id", productIds).set("supplier_code", supplierCode);
        productMapper.update(null, uw);
    }
}